<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/style.css"/>
<script type="text/javascript" src="${ctx}/js/xtable.js"></script>
<script type="text/javascript" src="${ctx}/js/xtree.js"></script>
<script type="text/javascript" src="${ctx}/js/validator.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/SecurityService.js"></script>
<script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body onload="init()">
<table>
    <tr>
        <td valign="top">
            <table>
                <tr>
                    <td align="center">
                        <form id="roleForm">
                        <table style="width:400px">
                            <tr>
                                <td>
                                    角色名称
                                </td>
                                <td>
                                    <input type="text" id="roleName" elname="角色名称" required="1" maxLen="100"/>
                                </td>
                                <td align="center">
                                        <img src="${ctx}/images/common/button_insert.jpg" title="添加角色" style="cursor:pointer" onclick="insertRole()"/>
                                      <img src="${ctx}/images/common/button_update.jpg" title="修改角色" style="cursor:pointer" onclick="updateRole()"/>
                                      <img src="${ctx}/images/common/button_delete.jpg" title="删除角色" style="cursor:pointer" onclick="deleteRole()"/>
                                </td>
                            </tr>
                        </table>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td>
                        <SCRIPT LANGUAGE="JavaScript">
                        var roleThead = new Array(
                        new XThead("选择", "radio"),
                        new XThead("角色ID"),
                        new XThead("角色名称")
                        );
            
                        var roleXTable = new XTable("role", roleThead, false);
            
                        roleXTable.XTbodyArray = function (model) {
                            var array = new Array(
                            new XTbody("role", model.roleId, "radio", "onclick='setRole(this)'", model),
                            new XTbody("role", model.roleId),
                            new XTbody("role", model.roleName)
                            );
                            return array;
                        }
            
                        roleXTable.getXList = function (page){
                            var role = new Role();
                            role.pageIndex = page;
                            role.pageSize = roleXTable.pageSize;
                            SecurityService.selectRoleList(role, function(list){roleXTable.addTable(list);});
                        }
                        function getRolePage(){
                            var role = new Role();
                            SecurityService.selectRoleNum(role, function(num){roleXTable.setTotalPage(num);}) ;
                        }
                        </SCRIPT>
                    </td>
                </tr>
            </table>
        </td>
        <td>
        &nbsp;
        </td>
        <td valign="top">
            <SCRIPT language="javascript" type="">
            var resourceTree = new WebFXTree(0, "资源");
            </SCRIPT>
        </td>
    </tr>
</table>
</body>
</html>
<script type="text/javascript">
function init(){
    getRolePage();
    var application = new Application();
    application.pageIndex = 1;
    application.pageSize = 100;
    SecurityService.selectApplicationResourceList(application, callbackSelectResourceList);
}
//解析返回的结果
function callbackSelectResourceList(list){
    var application;
    var resourceList;
    for(var i = 0; i < list.length; i++){
        application = list[i];
        var node = new WebFXTreeItem(application.applicationId, application.applicationName, "", null, resourceTree, false, false);
        resourceList = application.resourceList;
        addResourceNode(resourceList, node)
    }
    resourceTree.expandAll();
}

function addResourceNode(list, parentNode){
    for(var i = 0; i < list.length; i++){
        var resource = list[i];
        var node = new WebFXTreeItem(resource.resourceId, resource.resourceName, "", parentNode, resourceTree, true, true);
        var resourceList = resource.resourceList;
        if(resourceList != null && resourceList.length > 0){
            addResourceNode(resourceList, node);
        }
    }
}

function setRole(radio){
    var role = radio.parentElement.parentElement.model;
    dwr.util.setValue("roleName", role.roleName);
    var roleResource = new RoleResource();
    roleResource.roleId = role.roleId;
    SecurityService.selectRoleResourceList(roleResource, callbackSelectRoleResourceList);
}

function callbackSelectRoleResourceList(list){
    var resourceIds = "";
    for(var i = 0; i < list.length; i++){
        resourceIds += list[i] + ",";
    }
    if(resourceIds.length > 0){
        resourceIds = resourceIds.substring(0, resourceIds.length - 1);
    }
    resourceTree.setCheckNodeId(resourceIds);
}

function insertRole(){
    var _form = $("roleForm");
    if(!validate(_form)){
        return false;
    }
    var role = new Role();
    role.roleName = dwr.util.getValue("roleName");
    role.pageIndex = 1;
    role.pageSize = 1;
    SecurityService.selectRoleList(role, callbackSelectRoleList);
    
}

function callbackSelectRoleList(list){
    if(list.length > 0){
        alert("相同的角色名不允许重复添加！");
        callbackRole();
        return false;
    }
    var role = new Role();
    role.roleName = dwr.util.getValue("roleName");
    SecurityService.insertRole(role, callbackRole);
}

function updateRole(){
    var _form = $("roleForm");
    if(!validate(_form)){
        return false;
    }

    var roleId = roleXTable.getRadioValue();
    if(!roleId){
        alert("请选择你要更新的角色");
        return false;
    }
    if(!confirm("你确定要更新此角色信息")){
        return false;
    }
    var role = new Role();
    role.roleId = roleId;
    role.roleName = dwr.util.getValue("roleName");
    SecurityService.updateRole(role, callbackRole);
    var roleResource = new RoleResource();
    roleResource.roleId = roleId;
    var resourceIds = resourceTree.getCheckNodeId();
    SecurityService.insertRoleResource(roleResource, resourceIds);
}

function deleteRole(){
    var roleId = roleXTable.getRadioValue();
    if(!roleId){
        alert("请选择你要删除的角色");
        return false;
    }
    if(!confirm("你确定要删除此角色信息")){
        return false;
    }
    var role = new Role();
    role.roleId = roleId;
    SecurityService.deleteRole(role, callbackRole);
}

function callbackRole(){
    getRolePage();
    $("roleForm").reset();
    document.getElementById("roleName").focus();
}

</script>
